from ronglian_sms_sdk import SmsSDK
from django.conf import settings
from lyapi.settings import contains
import json
import logging
logger = logging.getLogger('django')


def send_message(phone,code):
    accId = settings.SMS_SETTINGS.get('accId')
    accToken = settings.SMS_SETTINGS.get('accToken')
    appId = settings.SMS_SETTINGS.get('appId')

    sdk = SmsSDK(accId, accToken, appId)
    # tid = '1'
    tid = settings.SMS_SETTINGS.get('tid')
    # mobile = phone
    datas = (code, contains.SMS_EXIPRE_TIME)
    resp = sdk.sendMessage(tid, phone, datas)
    print(resp)
    ret = json.loads(resp)
    statusCode = ret.get('statusCode')
    if statusCode == '000000':
        return True
    else:
        logger.error('%s短信发送有误,错误吗为%s' % (phone,statusCode))
        return False

    # return ret.get('statusCode')